const Tool = {
	getTimestamp: (isMicro: boolean = false): number => {
		let _timestamp = new Date().valueOf()
		if (isMicro) return _timestamp
		_timestamp = _timestamp / 1000
		_timestamp = Math.floor(_timestamp)
		return _timestamp
	},
	removeEmptyObjValues(obj: any): any {
		let result: any = {}
		for (const key in obj) {
			if (!(obj[key] == null || obj[key] == undefined || obj[key] == '')) {
				result[key] = obj[key]
			}
		}
		result = JSON.parse(JSON.stringify(result))
		return result
	},
	deepClone(param: any): any {
		return JSON.parse(JSON.stringify(param))
	},
	// 数组中是否有重复的元素
	hasDuplicates(array: any[]): boolean {
		return new Set(array).size !== array.length
	},
	base64toBlob: (base64Data: string): Blob => {
		let byteString
		if (base64Data.split(',')[0].indexOf('base64') >= 0)
			byteString = atob(base64Data.split(',')[1]) //base64 解码
		else {
			byteString = unescape(base64Data.split(',')[1])
		}
		const mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0] //mime类型 -- image/png
		const ia = new Uint8Array(byteString.length) //创建视图
		for (let i = 0; i < byteString.length; i++) {
			ia[i] = byteString.charCodeAt(i)
		}
		return new Blob([ia], {
			type: mimeString
		})
	},
	blobToBase64(blobUrl: string): Promise<string> {
		// const blob = new Blob(['Hello, world!'], { type: 'text/plain' })
		return new Promise((resolve, reject) => {
			fetch(blobUrl)
				.then(res => res.blob())
				.then(blob => {
					const reader = new FileReader()
					reader.readAsDataURL(blob)
					reader.onloadend = () => resolve(reader.result as string)
					reader.onerror = reject
				})
		})
	},
	formatMoney(amount: number): string {
		return new Intl.NumberFormat('zh-CN', {
			style: 'currency',
			currency: 'CNY',
			minimumFractionDigits: 0,
			maximumFractionDigits: 0
		}).format(amount)
	}
}
export default Tool
